/**
 * 
 */
package nl.boektrust.btsoep.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;

/**
 * @author rao3
 *
 */
public  class EditBookAfmetingenPanel extends EditBookPanel implements ClickHandler, ValueChangeHandler, KeyPressHandler {
	private int widthInMilimeters = -1;
	private int heightInMilimeters = -1;
	private int thicknessInMilimeters = -1;
	private TextBox widthTextBox  = new TextBox();
	private TextBox heightTextBox  = new TextBox();
	private TextBox thicknessTextBox  = new TextBox();
	//private Label theLabel;
	private Button xButton = new Button("x");;
	private EditBookDialog parentDialog;

	public EditBookAfmetingenPanel(String label, int widthInMilimeters, int heightInMilimeters, int thicknessInMilimeters, boolean isInEditMode, EditBookDialog parentDialog) {
		super(label, isInEditMode);
		this.widthInMilimeters = widthInMilimeters;
		this.heightInMilimeters = heightInMilimeters;
		this.thicknessInMilimeters = thicknessInMilimeters;

		Label aLabel = new Label("hoogte (mm): ");
		aLabel.setTitle("De hoogte van een traditioneel boek wordt verticaal over de kaft gemeten.");
		editPanel.add(aLabel);

		heightTextBox.setSize("5em", "1em");
		heightTextBox.addKeyPressHandler(this);
		heightTextBox.addValueChangeHandler(this);
		editPanel.add(heightTextBox);
		
		aLabel = new Label("breedte (mm): ");
		aLabel.setTitle("De breedte van een traditioneel boek wordt horizontaal over de kaft gemeten.");
		editPanel.add(aLabel);
		
		widthTextBox.setSize("5em", "1em");
		widthTextBox.addKeyPressHandler(this);
		widthTextBox.addValueChangeHandler(this);
		editPanel.add(widthTextBox);

		aLabel = new Label("dikte (mm): ");
		aLabel.setTitle("De dikte van een traditioneel boek wordt over een van de zijden gemeten.");
		editPanel.add(aLabel);

		thicknessTextBox.setSize("5em", "1em");
		thicknessTextBox.addKeyPressHandler(this);
		thicknessTextBox.addValueChangeHandler(this);
		editPanel.add(thicknessTextBox);
		editPanel.add(xButton);
		xButton.addClickHandler(this);

		this.parentDialog = parentDialog;
		if (isInEditMode == true) {
			setToEditMode();
		} else {
			setToDisplayMode();
		}
	}
	
	public EditBookAfmetingenPanel(String label, EditBookDialog parentPanel) {
		// set all dimensions to -1 (= unspecified)
		this(label, -1, -1, -1, false, parentPanel);
	}
	
	public EditBookAfmetingenPanel(String label, boolean isInEditMode) {
		// set all dimensions to -1 (= unspecified)
		this(label, -1, -1, -1, isInEditMode, null);
	}

	public EditBookAfmetingenPanel(boolean isInEditMode) {
		this("", isInEditMode);
	}

	public EditBookAfmetingenPanel(String label) {
		this(label, false);
	}

	public EditBookAfmetingenPanel() {
		this(false);
		System.out.println("Constructor EditBoekAfmetingenPanel() called");
	}
	

	/**
	 * @return the widthInMilimeters
	 */
	public int getWidthInMilimeters() {
		return widthInMilimeters;
	}

	/**
	 * @param widthInMilimeters the widthInMilimeters to set
	 */
	public void setWidthInMilimeters(int widthInMilimeters) {
		this.widthInMilimeters = widthInMilimeters;
	}

	/**
	 * @return the heightInMilimeters
	 */
	public int getHeightInMilimeters() {
		return heightInMilimeters;
	}

	/**
	 * @param heightInMilimeters the heightInMilimeters to set
	 */
	public void setHeightInMilimeters(int heightInMilimeters) {
		this.heightInMilimeters = heightInMilimeters;
	}

	/**
	 * @return the thicknessInMilimeters
	 */
	public int getThicknessInMilimeters() {
		return thicknessInMilimeters;
	}

	/**
	 * @param thicknessInMilimeters the thicknessInMilimeters to set
	 */
	public void setThicknessInMilimeters(int thicknessInMilimeters) {
		this.thicknessInMilimeters = thicknessInMilimeters;
	}

	boolean hasInvalidData() {
		return false;
	}



	@Override
	void setToEditMode() {
		super.setToEditMode();
		if (heightInMilimeters > -1) heightTextBox.setValue(new Integer(heightInMilimeters).toString()); 
		if (thicknessInMilimeters > -1) thicknessTextBox.setValue(new Integer(thicknessInMilimeters).toString()); 
		if (widthInMilimeters > -1) widthTextBox.setValue(new Integer(widthInMilimeters).toString()); 
	}

	@Override
	void setToDisplayMode() {
		super.setToDisplayMode();
		
		String theString = new String("");
		if (thicknessInMilimeters == -1 && heightInMilimeters == -1 && widthInMilimeters == -1) { 
			theString = new String("Afmetingen niet ingevuld");
		} else {
			if (heightInMilimeters >= 0) {
				theString = theString.concat("hoogte: "+heightInMilimeters+" (mm); ");
			}
			if (widthInMilimeters >= 0) {
				theString = theString.concat("breedte: "+widthInMilimeters+" (mm); ");
			}
			if (thicknessInMilimeters >= 0) {
				theString = theString.concat("dikte: "+thicknessInMilimeters+" (mm); ");
			}
			
		}
		theLabel.setText(theString);
	}
	
	public void onClick(ClickEvent event) {
		System.out.println("Click!");
		if (event.getSource() ==theLabel) {		
			this.setToEditMode();
		} else if (event.getSource() == xButton) {
			setInEditMode(false);
			this.setToDisplayMode();
		}
	}

	public void onValueChange(ValueChangeEvent event) {
		if (event.getSource() == heightTextBox) {
			if (!heightTextBox.getText().equals("")) {
				heightInMilimeters = new Integer(heightTextBox.getText()).intValue();
				System.out.println("set heightInMilimeters to "+heightInMilimeters);
			} else {
				heightInMilimeters = -1;
			}
		} else if (event.getSource() == widthTextBox) {
			if (!widthTextBox.getText().equals("")) {
				widthInMilimeters = new Integer(widthTextBox.getText()).intValue();
			} else {
				widthInMilimeters = -1;
			}
		} else if (event.getSource() == thicknessTextBox) {
			if (!thicknessTextBox.getText().equals("")) {
				thicknessInMilimeters = new Integer(thicknessTextBox.getText()).intValue();
			} else {
				thicknessInMilimeters = -1;
			}
		}
		if (parentDialog != null) parentDialog.panelHasChanged();
		
	}

	public void onKeyPress(KeyPressEvent event) {
		System.out.println("key: "+(int)event.getCharCode());
		if (event.getSource() == thicknessTextBox || event.getSource() == heightTextBox || event.getSource() == widthTextBox) {
			if (Character.isLetter(event.getCharCode()) || Character.isSpace(event.getCharCode())) {
				((TextBox) event.getSource()).cancelKey();
			}
		}

	}

	


}
